<?php

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

$url_form = wp_nonce_url(
    add_query_arg(
        [
            'action' => 'falcon5_save_settings',
        ],
        admin_url( 'admin-post.php' )
    ),
    'falcon5_save_settings'
);

$signup_link = esc_url( 'https://dashboard.weglot.com/register-wordpress' );
$data = get_option(FALCON5_NAME);
global $fc5_api_key;

$options_available = apply_filters(
    'weglot_tabs_admin_options_available', [
        'evn' => [
            'key'         => 'evn',
            'label'       => __( 'Evn', 'falcon5' ),
            'value'       => $data['falcon5-translate']['evn']??'',
            'description' => '',
        ],
        'api_key_private' => [
            'key'         => 'api_key_private',
            'label'       => __( 'API Key', 'falcon5' ),
            'value'       => $fc5_api_key,
            'description' => sprintf( esc_html__( 'Log in to %1$sWeglot%2$s to get your API key.', 'weglot' ), '<a target="_blank" href="' . $signup_link . '">', '</a>' ),
        ],
        'language_from'   => [
            'key'         => 'original_language',
            'label'       => __( 'Original language', 'falcon5' ),
            'description' => 'What is the original (current) language of your website?',
        ],
        'languages'       => [
            'key'         => 'destination_language',
            'label'       => __( 'Destination languages', 'falcon5' ),
            'description' => sprintf( esc_html__( 'Choose languages you want to translate into. Supported languages can be found %1$shere%2$s.', 'weglot' ), '<a target="_blank" href="https://weglot.com/documentation/available-languages/">', '</a>' ),
        ],
    ]
);

$lang_array = Fc5SupportLanguages();
$original_languages_possible = $lang_array;
$translateEvn = [
        'local'=>Fc5Translate( 'local'),
        'dev'=>Fc5Translate( 'dev'),
        'test'=>Fc5Translate( 'test'),
        'pre'=>Fc5Translate( 'pre'),
        'prod'=>Fc5Translate( 'prod'),
];
?>

<div id="wrap-falcon5">
    <div class="wrap">
        <form method="post" id="mainform" action="<?php echo esc_url( $url_form ); ?>">
            <h3 id="main_configuration"><?php esc_html_e( 'Main configuration', 'weglot' ); ?></h3>
            <hr>
            <table class="form-table">
                <tbody>
                <tr valign="top">
                    <th scope="row" class="titledesc">
                        <label for="<?php echo esc_attr( $options_available['evn']['key'] ); ?>">
                            <?php echo esc_html( $options_available['evn']['label'] ); ?>
                        </label>
                        <p class="sub-label"><?php echo $options_available['evn']['description']; //phpcs:ignore ?></p>
                    </th>
                    <td class="forminp forminp-text">
                        <select
                                class="weglot-select weglot-select-original"
                                name="<?php echo esc_attr( sprintf( '%s[%s]', FALCON5_SLUG, 'evn' ) ); ?>"
                                id="<?php echo esc_attr( $options_available['evn']['key'] ); ?>"
                        >
                            <?php
                            $evn = 'local';

                            foreach ( $translateEvn as $key => $evn_ ) {
                                if ( $key !== 'br' ) {
                                    ?>
                                    <?php if ( 1==1/* $this->options['has_first_settings'] */ ) { ?>
                                        <option
                                                value="<?php echo esc_attr( $key ); ?>"
                                            <?php selected( $key, $evn ); ?>
                                        >
                                            <?php esc_html_e( $evn_, 'falcon5' ); //phpcs:ignore ?>
                                        </option>
                                        <?php
                                    } else { ?>
                                        <option
                                                value="<?php echo esc_attr( $key ); ?>"
                                            <?php selected( $key, $this->options[ $options_available['evn']['key'] ] ); ?>
                                        >
                                            <?php esc_html_e( $evn_, 'falcon5' ); //phpcs:ignore ?>
                                        </option>
                                    <?php }
                                }
                            }
                            ?>
                        </select>
                    </td>
                </tr>
                <tr valign="top">
                    <th scope="row" class="titledesc">
                        <label for="<?php echo esc_attr( $options_available['api_key_private']['key'] ); ?>">
                            <?php echo esc_html( $options_available['api_key_private']['label'] ); ?>
                        </label>
                        <p class="sub-label"><?php echo $options_available['api_key_private']['description']; //phpcs:ignore ?></p>
                    </th>
                    <td class="forminp forminp-text">
                        <input
                                name="<?php echo esc_attr( sprintf( '%s[%s]', FALCON5_SLUG, $options_available['api_key_private']['key'] ) ); ?>"
                                id="<?php echo esc_attr( $options_available['api_key_private']['key'] ); ?>"
                                type="text"
                                required
                                placeholder="wg_XXXXXXXXXXXX"
                                value="<?= $fc5_api_key?:''; ?>"
                        >
                        <br>

                        <p class="description"><?php echo esc_html_e( 'If you don\'t have an account, you can create one in 20 seconds !', 'weglot' ); ?></p>

                    </td>
                </tr>

                <tr valign="top">
                    <th scope="row" class="titledesc">
                        <label for="<?php echo esc_attr( $options_available['language_from']['key'] ); ?>">
                            <?php echo esc_html( $options_available['language_from']['label'] ); ?>
                        </label>
                        <p class="sub-label"><?php echo $options_available['language_from']['description']; //phpcs:ignore ?></p>
                    </th>
                    <td class="forminp forminp-text">
                        <select
                                class="weglot-select weglot-select-original"
                                name="<?php echo esc_attr( sprintf( '%s[%s]', FALCON5_SLUG, 'language_from' ) ); ?>"
                                id="<?php echo esc_attr( $options_available['language_from']['key'] ); ?>"
                        >
                            <?php
                            $wplang = 'en';
                            if ( ! empty( get_option( 'WPLANG' ) ) ) {
                                $wplang = substr( get_option( 'WPLANG' ), 0, 2 );
                            }

                            foreach ( $original_languages_possible as $key => $language ) {
                                if ( $key !== 'br' ) {
                                    ?>
                                    <?php if ( 1==1/* $this->options['has_first_settings'] */ ) { ?>
                                        <option
                                                value="<?php echo esc_attr( $key ); ?>"
                                            <?php selected( $key, $wplang ); ?>
                                        >
                                            <?php esc_html_e( $language, 'falcon5' ); //phpcs:ignore ?>
                                        </option>
                                        <?php
                                    } else { ?>
                                        <option
                                                value="<?php echo esc_attr( $key ); ?>"
                                            <?php selected( $key, $this->options[ $options_available['language_from']['key'] ] ); ?>
                                        >
                                            <?php esc_html_e( $language, 'weglot' ); //phpcs:ignore ?>
                                        </option>
                                    <?php }
                                }
                            }
                            ?>
                        </select>
                    </td>
                </tr>

                <tr valign="top">
                    <th scope="row" class="titledesc">
                        <label for="<?php echo esc_attr( $options_available['languages']['key'] ); ?>">
                            <?php echo esc_html( $options_available['languages']['label'] ); ?>
                        </label>
                        <p class="sub-label"><?php echo $options_available['languages']['description']; //phpcs:ignore ?></p>
                    </th>

                    <td class="forminp forminp-text">
                        <select
                                class="weglot-select weglot-select-destination"

                                name="<?php echo esc_attr( sprintf( '%s[languages][][language_to]', FALCON5_SLUG ) ); ?>"
                                id="<?php echo esc_attr( $options_available['languages']['key'] ); ?>"
                                multiple="multiple"
                                required
                        >
                            <?php
                            $languages             = $lang_array;
                            $destination_languages =  array('en'=>'English');
                            ?>

                            <?php foreach ( $languages as $key => $language ) : ?>
                                <option value="<?= esc_attr( $key ); ?>"
                                    <?php selected( true, in_array( $language, $destination_languages, true ) ); ?>
                                >
                                    <?= esc_html( $language ); ?>
                                </option>
                            <?php endforeach; ?>
                        </select>

                        <?php
                        if ( 1==1 ) {
                            ?>
                            <p class="description">
                                <?php // translators: 1 HTML Tag, 2 HTML Tag ?>
                                <?php echo sprintf( esc_html__( 'On the free plan, you can choose one language and use a maximum of 2000 words. If you need more, please %1$supgrade your plan%2$s.', 'weglot' ), '<a target="_blank" href="https://dashboard.weglot.com/billing/upgrade">', '</a>' ); ?>
                            </p>
                            <?php
                        } elseif ( isset( $user_info['plan_id'] ) && in_array( $user_info['plan_id'], $plans['starter_free']['ids'] ) ) { //phpcs:ignore
                            ?>
                            <p class="description">
                                <?php // translators: 1 HTML Tag, 2 HTML Tag ?>
                                <?php echo sprintf( esc_html__( 'On the Starter plan, you can choose one language. If you need more, please %1$supgrade your plan%2$s.', 'weglot' ), '<a target="_blank" href="https://dashboard.weglot.com/billing/upgrade">', '</a>' ); ?>
                            </p>
                            <?php
                        } elseif ( isset( $user_info['plan_id'] ) && in_array( $user_info['plan_id'], $plans['business']['ids'] ) ) { //phpcs:ignore
                            ?>
                            <p class="description">
                                <?php // translators: 1 HTML Tag, 2 HTML Tag ?>
                                <?php echo sprintf( esc_html__( 'On the Business plan, you can choose 3 languages. If you need more, please %1$supgrade your plan%2$s.', 'weglot' ), '<a target="_blank" href="https://dashboard.weglot.com/billing/upgrade">', '</a>' ); ?>
                            </p>
                            <?php
                        } elseif ( isset( $user_info['plan_id'] ) && in_array( $user_info['plan_id'], $plans['pro']['ids'] ) ) { //phpcs:ignore
                            ?>
                            <p class="description">
                                <?php // translators: 1 HTML Tag, 2 HTML Tag ?>
                                <?php echo sprintf( esc_html__( 'On the Pro plan, you can choose 5 languages. If you need more, please %1$supgrade your plan%2$s.', 'weglot' ), '<a target="_blank" href="https://dashboard.weglot.com/billing/upgrade">', '</a>' ); ?>
                            </p>
                            <?php
                        }
                        ?>
                    </td>
                </tr>

                </tbody>
            </table>
            <?php submit_button(); ?>
        </form>
    </div>
</div>
